package newdecoupling.staffemp.service.Impl;

import newdecoupling.staffemp.dao.Impl.StaffEmpImpl;
import newdecoupling.staffemp.pojo.User;
import newdecoupling.staffemp.service.StaffEmpService;

import newdecoupling.staffemp.dao.StaffEmpDao;
import newdecoupling.staffemp.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

@Component
public class StaffEmpServiceImpl implements StaffEmpService {

    @Autowired
    private StaffEmpDao staffEmpDao;

    @Override
    public List<User> findOne(String uname,String pword) {
        List<User> list = staffEmpDao.findOne();


        String url="jdbc:mysql://localhost:3306/wod01";
        String username = "root";
        String password = "123456";
        // 1. 使用 ? 作为占位符
        String sql = "SELECT username, password FROM staff_user WHERE username = ? AND password = ?";

        Connection connection = null;
        Statement statement = null;
        ResultSet resultSet = null;

        CallableStatement callableStatement = null;
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            connection = DriverManager.getConnection(url, username, password);
//            statement = connection.createStatement();
            callableStatement = connection.prepareCall(sql);

//            设置参数值
            callableStatement.setString(1, uname);
            callableStatement.setString(2, pword);

//            resultSet = statement.executeQuery();
            resultSet = callableStatement.executeQuery();

            while (resultSet.next()){
                User user = new User();
                user.setUsername(resultSet.getString("username"));
                user.setPassword(resultSet.getString("password"));
                list.add(user);
            }
            return list;
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            try {
                if (resultSet != null) {
                    resultSet.close();
                }
                if (statement != null) {
                    statement.close();
                }
                if (connection != null) {
                    connection.close();
                }
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
    }


}
